package com.tg.appcommon.helper;

import android.app.Activity;
import android.net.ConnectivityManager;
import android.net.Network;
import android.os.Build;
import android.text.TextUtils;

import com.tange.base.toolkit.WifiAp;
import com.tange.module.core.wifi.scan.ApScanConfiguration;
import com.tg.appcommon.android.AbLogUtil;
import com.tange.base.toolkit.NetworkUtil;
import com.tg.appcommon.android.PermissionUtil;
import com.tg.appcommon.android.TGApplicationBase;
import com.tg.appcommon.android.TGLog;

public class ConnectivityManagerHelper {


    private volatile static ConnectivityManagerHelper helper;
    private ConnectivityManager connectivityManager;
    private ConnectivityManager.NetworkCallback networkCallback;
    private static final String TAG = "ConnectivityManagerHelper";

    private void  create() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            connectivityManager = TGApplicationBase.getApplication().getSystemService(ConnectivityManager.class);
            networkCallback = new ConnectivityManager.NetworkCallback() {
                @Override
                public void onAvailable(Network network) {
                    Network defaultNetwork = ConnectivityManager.getProcessDefaultNetwork();
                    AbLogUtil.d(TAG, "The getProcessDefaultNetwork   is now: " + defaultNetwork);
                    PermissionUtil permissionUtil = new PermissionUtil(TGApplicationBase.getApplication());
                    boolean isCheckOK = permissionUtil.check(android.Manifest.permission.ACCESS_FINE_LOCATION) &&
                            permissionUtil.check(android.Manifest.permission.ACCESS_COARSE_LOCATION);
                  if (!isCheckOK)return;
                    WifiAp wifiAp = NetworkUtil.getConnectWifiSSID(TGApplicationBase.getApplication());
                    if (!TextUtils.isEmpty(wifiAp.ssid) && wifiAp.ssid.startsWith(ApScanConfiguration.INSTANCE.apNamePrefix())) {
                        return;
                    }
                if (defaultNetwork!=null){
                    ConnectivityManager.setProcessDefaultNetwork(null);
                }
                }
            };
        }
    }


    public static ConnectivityManagerHelper getInstance() {
        if (helper == null) {
            synchronized (ConnectivityManagerHelper.class) {
                if (helper == null) {
                    helper = new ConnectivityManagerHelper();
                }
            }
        }
        return helper;
    }

    public void register() {
         Activity activity = TGApplicationBase.getAppLifecycleCallbacks().getCurrentActivity();
        if (activity==null)return;
        if (activity.getComponentName().getClassName().contains(".device.add"))return;
        create();
        if (connectivityManager != null && networkCallback != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                connectivityManager.registerDefaultNetworkCallback(networkCallback);
            }
        }
    }

    public void unregister() {
        if (connectivityManager != null && networkCallback != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                try {
                    connectivityManager.unregisterNetworkCallback(networkCallback);
                } catch (Throwable ex) {
                    TGLog.i(TAG, "unregister: error " + ex);
                }
            }
            connectivityManager = null;
            networkCallback = null;
        }
    }

}
